package com.atguigu.netty.http;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

import java.net.URI;

/**
 * 1.SimpleChannelInboundHandler是ChannelInboundHandlerAdapter的子类
 * 2.HttpObject表示客户端和服务器端相互通讯的数据被封装成HttpObject
 */
public class TestHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {
    //读取客户端数据
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws Exception {

        System.out.println("对应的channel="+channelHandlerContext.channel()+" pipeline "+channelHandlerContext.pipeline());

        System.out.println("当前channelHandlerContext的handler = "+channelHandlerContext.handler());

        //httpObject 客户端发过来的数据
        //判断httpObject 是不是 httpRequest请求
        if(httpObject instanceof HttpRequest){
            System.out.println("channelHandlerContext类型 = "+channelHandlerContext.getClass());

            System.out.println("pipeline hashCode = "+channelHandlerContext.pipeline().hashCode()+"  TestHttpServerHandler hashCode = "+this.hashCode());

            System.out.println("httpObject类型 = "+httpObject.getClass());
            System.out.println("客户端地址 = "+channelHandlerContext.channel().remoteAddress());

            //获取到
            HttpRequest httpRequest= (HttpRequest) httpObject;
            //获取uri
            URI uri = new URI(httpRequest.uri());
            if("/favicon.ico".equals(uri.getPath())){
                System.out.println("请求了favicon.ico,不做响应");
                return;
            }

            //回复信息给浏览器【http协议】
            ByteBuf content= Unpooled.copiedBuffer("hello,我是服务器", CharsetUtil.UTF_8);
            //构造一个http响应
            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain;charset=utf-8");
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());

            //将构建好的response返回
            channelHandlerContext.writeAndFlush(response);
        }
    }
}
